<?php

namespace app\admin\controller;

use app\admin\controller\Common;
use think\Db;
use think\facade\Session;
use think\facade\Cache;

class Index extends Common
{

    public function index()
    {
        $admin = Session::get('admin', 'mmjtAdmin');

        if ($admin['id'] != 1 && $admin['type'] != 1) {
            $auth_visit = Db::name('admin_type')->where(['id' => $admin['type']])->cache(_cache('db'))->value('data');
            $auth_visit = json_decode($auth_visit, true);
        } else {
            $auth_visit = Db::name('category')->cache(_cache('db'))->column('id');
        }

        $platform = sysConfig('platform');
		$easemob    = sysConfig('easemob');
        $customer_service_member = model('common/member/Member')->getInfoByID($platform['customer_service_member_id'] ?? 1, 'id,nickname,avatar');
		$this->assign('easemob', $easemob); //
        $this->assign('customer_service_member', $customer_service_member); //
        $this->assign('is_customer_service', $admin['is_customer_service'] ?? 0);
        $this->assign('admin', $admin);
        $this->assign('auth_visit', $auth_visit); //改动最小得写法
        return $this->fetch();
    }

    //退出登陆
    public function out()
    {
        session(null, 'mmjtAdmin');
        $this->redirect('login/index');
    }

    public function main()
    {
        $arr = [
            'model' => [
                'member' => model('common/member/Member'),
                'content' => model('common/content/Content'),
                'comment' => model('common/content/Comment')
            ],
            'time' => [
                'today' => [['', 'exp', Db::raw('FROM_UNIXTIME(`create_time`,\'%Y-%m-%d\') = \'' . date('Y-m-d') . '\'')]],
                'week' => [['', 'exp', Db::raw('FROM_UNIXTIME(`create_time`,\'%Y-%v\') = \'' . date('Y-W') . '\'')]],
                'month' => [['', 'exp', Db::raw('FROM_UNIXTIME(`create_time`,\'%Y-%m\') = \'' . date('Y-m') . '\'')]],
                'all' => []
            ],
        ];
        $data = [];
        foreach ($arr['model'] as $item => $model) {
            foreach ($arr['time'] as $key => $where) {
                $data[$item][$key] = $model->getCount($where);
            }
        }


        $pending_list = [
            [
                'name' => '激活申请',
                'count' => model('common/member/MemberApply')->getCount(['state' => 0]),
                'url' => url('Member/applyList', ['field' => 'state', 'value' => 0])],
            [
                'name' => '提现',
                'count' => model('common/withdraw/Withdraw')->getCount(['state' => 0]),
                'url' => url('Withdraw/index', ['field' => 'state', 'value' => 0])],
            [
                'name' => '举报',
                'count' => model('common/report/ReportUser')->getCount(['state' => 0]),
                'url' => url('Report/user', ['field' => 'ru.state', 'value' => 0])
            ],
            [
                'name' => '反馈',
                'count' => model('common/feedback/Feedback')->getCount(),
                'url' => url('Feedback/feedbacklist')
            ],
        ];


        $comment = $arr['model']['comment']->getContentCommentList([], 5, 'c_c.content,c_c.create_time,m.nickname', 'c_c.create_time desc');
        $this->assign('comment', $comment);
        $this->assign('data', $data);
        $this->assign('pending_list', $pending_list);
        return $this->fetch();
    }

    //清空缓存
    public function cache_del()
    {
        Cache::clear();
        return ['ret' => '清空成功'];
    }

    //icon 图标
    public function fontawesome()
    {
        return $this->fetch();
    }


    public function upload()
    {
        $type = input('type', 'upload');
        $root_path = ROOT_PATH . 'public';
        switch ($type) {
            case 'admin':
                $path = $root_path . '/style/admin/img';
                $saveName = 'profile_small.jpg';
                break;
            default:
                $path = $root_path . DS . 'disk' . DS . $type;
                $saveName = true;
        }

        if (!file_exists($path)) {
            mkdir($path, 0755);
        }

        if ($file = $this->request->file('image')) {
            $info = $file->validate(['ext' => 'jpg,png,gif,jpeg,bmp'])->move($path, $saveName);
            if ($info) {

                if (!in_array($type, ['avatar'])) {
                    $absoluteUrl = str_replace($root_path, '', $path) . DS . $info->getSaveName();
                    $realPath = ROOT_PATH . DS . 'public' . DS . $absoluteUrl;

                    $image = \think\Image::open($realPath);
                    // 给原图左上角添加水印并保存water_image.png
                    $image->water(ROOT_PATH . DS . 'public' . config('app.fixed_image_path.water_image'), \think\Image::WATER_SOUTHEAST)->save($realPath);
                }


                return str_replace($root_path, '', $path) . DS . $info->getSaveName();
            }
            return json(['err' => $info->getError()]);
        }
        return $this->fetch();
    }

    public function uploadKey()
    {
        $type = input('type');

        $root_path = ROOT_PATH;
        $path = $root_path . DS . 'alipay_key';
        $saveName = $type;
        if (!file_exists($path)) mkdir($path, 0755);
        if ($file = $this->request->file($type)) {
            $info = $file->validate(['ext' => 'txt,pem'])->move($path, $saveName);
            if ($info) {
                $url = str_replace($root_path, '', $path) . DS . $info->getSaveName();
                return json_encode(ds_arr(200, '上传成功', $url));
            }
            return json_encode(ds_arr(400, $file->getError()));
        }
        return json_encode(ds_arr(400, '上传失败', '上传失败'));
    }
}
